home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-02 | 2.6 KB | 141 lines | [TEXT/CWIE] |
- /*
- File: ThreadSupport.cp
-
- Contains: stuff
-
- */
-
- #include "ThreadSupport.h"
- #include "Exceptions.h"
- #include "Futures.h"
- #include <Gestalt.h>
-
-
- LinkedListOf<TThread> TThread::fgThreadList; // Global thread list
-
-
- TThread::TThread(ThreadID id) : fThreadID(id)
- {
- #if !GENERATINGCFM
- fSavedA5 = SetCurrentA5();
- #endif
-
- #if CW7_EXCEPTIONS
- // set up interface to C++ exception runtime support
- __new_exception_state(&fExceptionState, fCatchBuffer, sizeof(fCatchBuffer));
- #endif
-
- FailOSErr(SetThreadSwitcher( id, SwitchOutProc, this, true));
- FailOSErr(SetThreadSwitcher( id, SwitchInProc, this, false));
- FailOSErr(SetThreadTerminator(id, TerminationProc, this));
- }
-
-
- TThread::~TThread()
- {
- }
-
-
- void TThread::SwitchIn(void)
- {
- #if CW7_EXCEPTIONS
- // restore exception-handling context
- ExceptionState dummy;
- __switch_exception_state(&fExceptionState, &dummy);
- #endif
-
- IteratorForLinkedListOf<ThreadContext> iter(fContextList);
-
- for (ThreadContext* context = iter.First(); context; context = iter.Next())
- {
- context->SwitchIn();
- }
- }
-
-
- void TThread::SwitchOut(void)
- {
- {
- IteratorForLinkedListOf<ThreadContext> iter(fContextList);
-
- for (ThreadContext* context = iter.Last(); context; context = iter.Prev())
- {
- context->SwitchOut();
- }
- }
-
- #if CW7_EXCEPTIONS
- // save exception-handling context
- __switch_exception_state(&fExceptionState, &fExceptionState);
- #endif
- }
-
-
- void TThread::Terminate(void)
- {
- IteratorForLinkedListOf<ThreadContext> iter(fContextList);
-
- for (ThreadContext* context = iter.Last(); context; context = iter.Prev())
- {
- context->Terminate();
- }
- }
-
-
- pascal void TThread::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
- {
- TThread* thread = (TThread*) switchProcParam;
-
- #if !GENERATINGCFM
- long savedA5 = SetA5(thread->fSavedA5);
- #endif
-
- #if THREAD_PROFILE
- ::ProfilerSwitchToThread(thread->mProfilerRef);
- #endif
-
- thread->SwitchIn();
-
- #if !GENERATINGCFM
- SetA5(savedA5);
- #endif
- }
-
-
- pascal void TThread::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
- {
- TThread* thread = (TThread*) switchProcParam;
-
- #if !GENERATINGCFM
- long savedA5 = SetA5(thread->fSavedA5);
- #endif
-
- thread->SwitchOut();
-
- #if !GENERATINGCFM
- SetA5(savedA5);
- #endif
- }
-
-
- pascal void TThread::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
- {
- TThread* thread = (TThread*) terminationProcParam;
-
- #if !GENERATINGCFM
- long savedA5 = SetA5(thread->fSavedA5);
- #endif
-
- thread->Terminate();
-
- #if !GENERATINGCFM
- SetA5(savedA5);
- #endif
- }
-
- //--------------------------------------------------------------------------------
- void* TThread::GetObjectPtr()
- {
- return this;
- }
-